using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing;

namespace MSNClient
{
    public class Common_GUI_Data:Form
    {
        Timer fadeTimer;
        bool closeForm = false;
        bool allowClosing = false;
        //Sets to false if Opacity = 1.0;
        bool isTransparent;

        Double fadeThreshold;
        //Opacity of inactive form
        float inActiveFormOpacity = 0.5F;
        //Opacity of the active form
        float activeFormOpacity = 1.0F;
        //SystrayIcon
        protected NotifyIcon notifyIcon;
        String systrayIconText = "MsnClient - Offline";

        public Common_GUI_Data()
        {
            isTransparent = true;
            this.fadeTimer = new Timer();
            this.fadeTimer.Interval = 5;
            this.fadeTimer.Tick += new EventHandler(this.fadeTimer_Tick);

            //Show form on the top;
            this.TopLevel = true;
            //SystrayIcon
           
            notifyIcon = new NotifyIcon();
            notifyIcon.Icon = new Icon(GetType(), "messenger.ico");
            SystrayIconText(Form_Main.ApplicationName,"Offline");
            notifyIcon.MouseUp +=new MouseEventHandler(notifyIcon_MouseUp);
            notifyIcon.Visible = true;
        }

        void notifyIcon_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                MakeActive();
            }
        }
        public void MakeActive()
        {
            if (!this.Visible)
            {
                this.Visible = true;
            }
            if (this.WindowState != FormWindowState.Normal)
            {
                this.WindowState = FormWindowState.Normal;
            }
            this.Activate();
        }
        /// <summary>
        /// param1 - param2
        /// </summary>
        /// <param name="param1"></param>
        /// <param name="param2"></param>
        /// <returns></returns>
        public void SystrayIconText(String param1, String param2)
        {
            systrayIconText = param1 + " - " + param2;
            notifyIcon.Text = systrayIconText;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="aFadeThreshold">Value of the Opacity that must be</param>
        /// <param name="aOpacity">Start value of Opacity, usually this.Opacity</param>
        public void StartFading(double aFadeThreshold, double aOpacity)
        {
            fadeTimer.Stop();
            fadeThreshold = aFadeThreshold;
            this.Opacity = aOpacity;
            fadeTimer.Start();
        }
        protected override void OnLoad(EventArgs e)
        {
            StartFading(activeFormOpacity, 0.0);
            base.OnLoad(e);
        }
        protected override void OnDeactivate(EventArgs e)
        {
            if (!isTransparent)
            {
                StartFading(inActiveFormOpacity, this.Opacity);
            }
            else
            {
                isTransparent = (inActiveFormOpacity > (float)this.Opacity);
                StartFading(inActiveFormOpacity, this.Opacity);
            }
            base.OnDeactivate(e);
        }
        protected override void OnActivated(EventArgs e)
        {
            this.WindowState = FormWindowState.Normal;
            isTransparent = true;
            StartFading(activeFormOpacity, this.Opacity);
            this.ShowInTaskbar = true;
            base.OnActivated(e);
        }
        public void CloseMainForm()
        {
            allowClosing = true;
            this.Close();
        }
        protected override void OnClosing(CancelEventArgs e)
        {
            if (allowClosing)
            {
                Console.WriteLine("OnClosing");
                if (!closeForm)
                {
                    isTransparent = false;
                    e.Cancel = true;
                    closeForm = true;
                    StartFading(0.0, this.Opacity);
                }
            }
            else
            {
                e.Cancel = true;
                MinimizeMainForm();
            }

            base.OnClosing(e);
            
        }
        protected override void OnClosed(EventArgs e)
        {
            notifyIcon.Visible = false;
            notifyIcon.Dispose();
            base.OnClosed(e);
        }
        protected void MinimizeMainForm()
        {
            this.WindowState = FormWindowState.Minimized;
            //this.ShowInTaskbar = false;
            this.Visible = false;
        }
        //For FadeTimerOnly
        private void fadeTimer_Tick(object sender, System.EventArgs e)
        {
            if (!isTransparent)
            {
                if ((float)this.Opacity > fadeThreshold)
                {
                    this.Opacity -= 0.01;
                }
                else
                {
                    fadeTimer.Stop();
                    isTransparent = true;
                    if (closeForm)
                    {
                        Close();
                    }
                }
            }
            else
            {
                if((float)this.Opacity < fadeThreshold)
                {
                    Opacity += 0.01;
                }
                else
                {
                    fadeTimer.Stop();
                    isTransparent = false;
                }

            }
        }
    }
}
